import { REG_NAME } from "../common";
import { UserAppDetail, UserAppTree } from "../interface";

export const validteUserAppTree = (item: Partial<UserAppTree>, detail: UserAppDetail) => {
    const { tree } = detail
    if (!item.title || !REG_NAME.test(item.title)) {
        return "标题不能包含特殊字符"
    }
    let index = tree.findIndex(t => t.id === item.id)
    if (item.id && index === -1) {
        return "文档不存在"
    }
    let parentId = item.parentId
    for (let i = 0; i < tree.length; i++) {
        const ele = tree[i];
        if (
            ele.id != item.id &&
            (ele.parentId === parentId || parentId === undefined && ele.parentId === undefined) &&
            ele.title === item.title
        ) {
            return "当前目录存在同名文档"
        }
    }
}

export const validteUserAppTreeDelete = (treeId: number, detail: UserAppDetail) => {
    if (!detail.tree.find(item => item.id === treeId)) {
        return "文档不存在"
    }
    if (detail.tree.find(item => item.parentId === treeId)) {
        return "存在下级文档，不可删除"
    }
}